package com.supreme.omsserver.config;

import com.supreme.omsserver.config.props.RedisProps;
import io.netty.channel.nio.NioEventLoopGroup;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
public class RedissonConfig {

    @Resource
    private RedisProps redisProps;

    @Bean
    RedissonClient redissonClient() {
        String address = String.format("redis://%s:%s", redisProps.getHost(), redisProps.getPort());
        Config config = new Config()
                .setCodec(new JsonJacksonCodec())
                .setThreads(16)
                .setNettyThreads(32)
                .setEventLoopGroup(new NioEventLoopGroup())
                .setTransportMode(TransportMode.NIO);

        config.useSingleServer()
                .setAddress(address)
                .setPassword(redisProps.getPassword())
                .setDatabase(5)
                .setTimeout(Math.toIntExact(redisProps.getTimeout()))
                .setConnectionPoolSize(64)
                .setConnectionMinimumIdleSize(24);

        return Redisson.create(config);
    }
}